﻿//********************************************************************************//
//创建日期：<创建日期，2016-09-14>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：增井
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using Web.Class;
using System.Text;
using System.Text.RegularExpressions;

namespace Web.Pages.QuShui.DiXiaS.YongHuGL.HuJiGL
{
    public partial class ZengJing : BasePage
    {
        #region 属性
        /// <summary>
        /// gridView主键(用户信息ID)
        /// </summary>
        private int JINGXXID
        {
            get
            {
                return Convert.ToInt32(ViewState["_JINGXXID"]);
            }
            set
            {
                ViewState["_JINGXXID"] = value;
            }
        }
        /// <summary>
        /// 用户号
        /// </summary>
        private string YHH
        {
            get
            {
                return ViewState["_YHH"] as string;
            }
            set
            {
                ViewState["YHH"] = value;
            }
        }
        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 用户信息BLL
        /// </summary>
        QS_YG_YONGHUXXBLL _QS_YG_YONGHUXXBLL = new QS_YG_YONGHUXXBLL();
        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        /// <summary>
        /// 深井信息
        /// </summary>
        QS_YH_SHENJINGXXB _SJXX = new QS_YH_SHENJINGXXB();

        /// <summary>
        /// 深井信息BLL
        /// </summary>
        QS_YH_SHENJINGXXBBLL _SJXXBLL = new QS_YH_SHENJINGXXBBLL();


        QS_JX_XINGZHENGQBLL XZQ = new QS_JX_XINGZHENGQBLL();

        QS_JX_JINGBIAOJXXBLL JBJ = new QS_JX_JINGBIAOJXXBLL();


        QS_JX_LIUYUXXBLL LYXX = new QS_JX_LIUYUXXBLL();

        QS_JX_SHUILIFPXXBLL SLFP = new QS_JX_SHUILIFPXXBLL();

        QS_JX_SHUIZIYFQBLL SZYFQ = new QS_JX_SHUIZIYFQBLL();


        /// <summary>
        /// 日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                Display();
                LoadData();
                InitData();
               
            }
        }

       


        /// <summary>
        /// 放弃
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Init;
            Display();
            
        }

        /// <summary>
        /// 修改
        /// </summary>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Add;
            Display();
            if (txtYongHuH.Text.Trim() == "")
            {
                txtYongHuH.ClientEnabled = true;
                txtYongHuH.Focus();
            }
            else
            {
                txtYongHuH.ClientEnabled = false;
                txtShenJingBH.Focus();
            }

        }

        /// <summary>
        /// 保存
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                SaveData();
            }
        }

        /// <summary>
        /// 前台事件回调方法
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            string yhh = this.hdCollection.Get("yhh").ToString();

            if (e.Parameter == "setinfo")
            {
                string msg = "";

                if (yhh != "") {
                    DataTable dt = _QS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("I_JLZT=0 AND S_YONGHUH='{0}'", yhh), null);
                    msg = dt.Rows.Count > 0 ? dt.Rows[0]["S_YONGHUM"].ToString() : "";

                    //如果当前用户下有井，查询出井类型，同一个用户下的井只能是同一类型
                    DataTable dt1 = _SJXXBLL.SelectQS_YH_SHENJINGXXBsDynamic(string.Format("S_JINGBH IN (SELECT S_JINGBIANH from qs_yh_yonghusjgxb where i_jlzt=0 and s_yonghubh='{0}')", yhh), null);
                    msg += "|";
                    msg +=  dt1.Rows.Count > 0 ? dt1.Rows[0]["S_JINGLEIXBH"].ToString() : "0";
                   
                }
                e.Result = e.Parameter + "|0|" + msg;
            }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            //txtYongHuH.Text = "";
            //txtYONGHUM.Text = "";
            txtShenJingBH.Text = "";
            txtYuanShenJBH.Text = "";
            txtQuShuiDD.Text = "";
            cmbXingZhengQ.Text = "";
            cmbLiuYu.Text = "";
            cmbShuiZiYFQ.Text = "";
            cmbShuiLiFP.Text = "";
            cmbHuanXianBJ.Text = "";
            txtCengCi.Text = "";
            txtShenDu.Text = "";
            txtKouJing.Text = "";
            txtXuKeZ.Text = "";
            txtDu.Text = "";
            txtFen.Text = "";
            txtMiao.Text = "";
            txtDu2.Text = "";
            txtFen2.Text = "";
            txtMiao2.Text = "";
            cmbJingLei.Text = "";
            cmbJingBJ.Text = "";
            cmbHuanXianBJ.Text = "";
            txtBeiZhu.Text = "";
            //deRiQi_QS.Text = "";
            //deRiQi_ZZ.Text = "";
            txtXuKeSLH.Text="";
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            //加载行政区
            LoadXZQ();
            //加载环线标记
            LoadHXBJ();
            //加载井标记
            LoadJBJ();
            //加载流域
            LoadLY();
            //水利分片
            LoadSLFP();
            //水资源分区
            LoadSZYFQ();
            deRiQi_QS.Value = DateTime.Now;
            deRiQi_ZZ.Value = DateTime.Now;
        }



        /// <summary>
        /// 加载行政区
        /// </summary>
        private void LoadXZQ()
        {
            DataTable dt = XZQ.SelectQS_JX_XINGZHENGQsDynamic("I_JLZT=0", null);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cmbXingZhengQ.Items.Add(dt.Rows[i]["S_XINGZHENGQM"].ToString(), dt.Rows[i]["S_XINGZHENGQH"]);
            }
        }


        /// <summary>
        /// 加载环线标记
        /// </summary>
        private void LoadHXBJ()
        {
            cmbHuanXianBJ.Items.Add("外环线外", 2);
            cmbHuanXianBJ.Items.Add("外环线内", 1);
        }


        /// <summary>
        /// 加载井标记
        /// </summary>
        private void LoadJBJ()
        {
            DataTable dt = JBJ.SelectQS_JX_JINGBIAOJXXesDynamic("I_JLZT=0", null);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cmbJingBJ.Items.Add(dt.Rows[i]["S_YUZHIMC"].ToString(), dt.Rows[i]["S_YUZHIBH"]);
            }
        }

        /// <summary>
        /// 加载流域
        /// </summary>
        private void LoadLY()
        {
            DataTable dt = LYXX.SelectQS_JX_LIUYUXXesDynamic("I_JLZT=0", null);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cmbLiuYu.Items.Add(dt.Rows[i]["S_LIUYUMC"].ToString(), dt.Rows[i]["S_LIUYUBH"]);
            }
        }

        /// <summary>
        /// 加载水利分片
        /// </summary>
        private void LoadSLFP()
        {
            DataTable dt = SLFP.SelectQS_JX_SHUILIFPXXesDynamic("I_JLZT=0", null);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cmbShuiLiFP.Items.Add(dt.Rows[i]["S_SHUILIFPMC"].ToString(), dt.Rows[i]["S_SHUILIFPBH"]);
            }
        }

        /// <summary>
        /// 加载水资源分区
        /// </summary>
        private void LoadSZYFQ()
        {
            DataTable dt = SZYFQ.SelectQS_JX_SHUIZIYFQsDynamic("I_JLZT=0", null);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cmbShuiZiYFQ.Items.Add(dt.Rows[i]["S_SHUIZIYMC"].ToString(), dt.Rows[i]["S_SHUIZIYBH"]);
            }
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            deRiQi.Text = DateTime.Now.ToString("yyyy-MM-dd");
            string yonghuh = this.Request.QueryString["yonghuh"];
            if (yonghuh != "" && yonghuh != null)
            {
                this.YHH = yonghuh;
                txtYongHuH.Text = yonghuh;
                txtYongHuH.ClientEnabled = false;
                DataTable dt = _QS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("I_JLZT=0 AND S_YONGHUH='{0}'", yonghuh), null);
                txtYONGHUM.Text = dt.Rows.Count > 0 ? dt.Rows[0]["S_YONGHUM"].ToString() : "";
            }
        }

        /// <summary>
        ///用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (this.Status)
            {
                case GridViewStatus.Init:
                    btnEdit.Visible = true;
                    btnEdit.Enabled = true;
                    btnSave.Visible = false;
                    btnCancel.Visible = false;
                    SetText(false);
                    break;
                case GridViewStatus.Update:
                    btnEdit.Visible = false;
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    txtShenJingBH.ClientEnabled = false;
                    SetText(true);
                    break;
                case GridViewStatus.Add:
                    btnEdit.Visible = false;
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    //txtYongHuH.ClientEnabled = true;
                    //txtYongHuH.Focus();
                    InitControl();
                    SetText(true);
                    break;
            }
        }


        /// <summary>
        /// 设置输入框状态
        /// </summary>
        /// <param name="b"></param>
        private void SetText(bool b)
        {
            txtShenJingBH.ClientEnabled = b;
            txtYuanShenJBH.ClientEnabled = b;
            txtQuShuiDD.ClientEnabled = b;
            cmbXingZhengQ.ClientEnabled = b;
            cmbLiuYu.ClientEnabled = b;
            cmbShuiZiYFQ.ClientEnabled = b;
            cmbShuiLiFP.ClientEnabled = b;
            txtCengCi.ClientEnabled = b;
            txtShenDu.ClientEnabled = b;
            txtKouJing.ClientEnabled = b;
            txtXuKeZ.ClientEnabled = b;
            txtDu.ClientEnabled = b;
            txtFen.ClientEnabled = b;
            txtMiao.ClientEnabled = b;
            txtDu2.ClientEnabled = b;
            txtFen2.ClientEnabled = b;
            txtMiao2.ClientEnabled = b;
            cmbJingBJ.ClientEnabled = b;
            cmbHuanXianBJ.ClientEnabled = b;
            cmbJingLei.ClientEnabled = b;
            txtBeiZhu.ClientEnabled = b;
            txtBLL.ClientEnabled = b;
            txtYC.ClientEnabled = b;
            txtXuKeSLH.ClientEnabled = b;
            deRiQi_QS.ClientEnabled = b;
            deRiQi_ZZ.ClientEnabled = b;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private void SaveData()
        {
            try { 
            int wtype = 2;
            int slym = BusinessTool.GetSLYMorZWNY(1, wtype);
            int userid = LoginInfo.CurrentUser.UserID;
            OperateState operateState;
            //修改操作
            operateState = OperateState.Add;

            DataTable dtjh = _SJXXBLL.SelectQS_YH_SHENJINGXXBsDynamic(string.Format("i_jlzt=0 and S_JINGBH='{0}'", txtShenJingBH.Text), null);
            if (dtjh.Rows.Count > 0)
            {
                PageTools.AlertMsg(this, "此井编号已经被使用，请重新填写！");
                this.txtShenJingBH.Focus();
            }
            else
            {
                _SJXX.ID = 0;
                _SJXX.I_JLZT = 0;
                _SJXX.S_LAOBIANH = txtYuanShenJBH.Text;
                _SJXX.S_JINGBH = txtShenJingBH.Text;
                _SJXX.S_JINGDIZ = txtQuShuiDD.Text;
                _SJXX.S_XINGZHENGQBH = cmbXingZhengQ.Text != "" ? cmbXingZhengQ.Value.ToString() : "";
                _SJXX.S_LIUYUBH = cmbLiuYu.Text != "" ? cmbLiuYu.Value.ToString() : "";
                _SJXX.S_SHUIZIYBH = cmbShuiZiYFQ.Text != "" ? cmbShuiZiYFQ.Value.ToString() : "";
                _SJXX.S_FENGPIANBH = cmbShuiLiFP.Text != "" ? cmbShuiLiFP.Value.ToString() : "";
                _SJXX.S_HUANGXIANBH = cmbHuanXianBJ.Text != "" ? cmbHuanXianBJ.Value.ToString() : "";
                _SJXX.I_CENG = txtCengCi.Text != "" ? Convert.ToDecimal(txtCengCi.Text) : 0;
                _SJXX.S_SHENGDU = txtShenDu.Text;
                _SJXX.I_KONGJING = txtKouJing.Text != "" ? Convert.ToDecimal(txtKouJing.Text) : 0;
                _SJXX.S_XUKEZHENGBH = txtXuKeZ.Text;
                string zb = txtDu.Text + "/" + txtFen.Text + "/" + txtMiao.Text + "/" + txtDu2.Text + "/" + txtFen2.Text + "/" + txtMiao2.Text;
                _SJXX.S_ZHUOBIAO = zb;
                _SJXX.S_JINGBIAOSBH = cmbJingBJ.Text != "" ? cmbJingBJ.Value.ToString() : "";
                _SJXX.S_HUANGXIANBH = cmbHuanXianBJ.Text != "" ? cmbHuanXianBJ.Value.ToString() : "";
                _SJXX.S_BEIZHU = txtBeiZhu.Text;
                _SJXX.S_LAOBIANH2 = txtBaoFeiJBH.Text;
                _SJXX.D_JINGRQ = deRiQi.Text != "" ? (DateTime?)Convert.ToDateTime(deRiQi.Text) : null;
                _SJXX.S_KAISHINY = slym.ToString();
                _SJXX.S_CAOZUOR = userid.ToString();
                _SJXX.D_CAOZUORQ = DateTime.Now;
                _SJXX.S_TIANJIAR = userid.ToString();
                _SJXX.D_TIANJIARQ = DateTime.Now;
                _SJXX.I_BLL = txtBLL.Text != "" ? Convert.ToInt32(txtBLL.Text) : 0;
                _SJXX.I_YC = txtYC.Text != "" ? Convert.ToInt32(txtYC.Text) : 0;
                _SJXX.S_JINGLEIXBH = cmbJingLei.Text != "" ? cmbJingLei.Value.ToString() : null;
                _SJXX.S_JINGZHUANGTBH = ckbJingXZ.Checked ? "4" : "1";
                _SJXX.S_XUKESLH = txtXuKeSLH.Text != "" ? txtXuKeSLH.Text : "";
                _SJXX.D_XUKEQSSJ = deRiQi_QS.Text != "" ? (DateTime?)Convert.ToDateTime(deRiQi_QS.Text) : null;
                _SJXX.D_XUKEJSSJ = deRiQi_ZZ.Text != "" ? (DateTime?)Convert.ToDateTime(deRiQi_ZZ.Text) : null;
                _SYS_LOGBLL.Log<QS_YH_SHENJINGXXB>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "用户管理-增井", operateState, _SJXX, null);
                bool result = _SJXXBLL.InsertOrUpdate(_SJXX);
              
                //用户水井关系表也要更新数据
                string v = "";
               // PageTools.AlertMsg(this, @"WTYPE:" + wtype.ToString() + ";USERID:" + userid.ToString() + ";YONHUH:" + txtYongHuH.Text + ";JINGBH:" + txtShenJingBH.Text);
                _spHelper.P_QSYH_ZJORZB(wtype, 1, userid.ToString(), txtYongHuH.Text, txtShenJingBH.Text, out v);
              
                if (result && v == "1")
                {
                   
                    PageTools.AlertMsg(this, "保存成功！");
                    this.Status = GridViewStatus.Init;
                    Display();
                }
                else
                {
                    PageTools.AlertMsg(this, "保存失败！");
                }
            }
            }
            catch(Exception V ){
                PageTools.AlertMsg(this, V.ToString());
            }
        }

        /// <summary>
        /// 给输入框赋值
        /// </summary>
        private void SetData()
        {
            _SJXX = _SJXXBLL.SelectQS_YH_SHENJINGXXB(JINGXXID);
            DataTable dtyh = _QS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("s_yonghuh =(select s_yonghubh from QS_YH_YONGHUSJGXB where s_jingbianh='{0}' and i_jlzt=0) and i_jlzt=0", _SJXX.S_JINGBH), null);
            txtYongHuH.Text = dtyh.Rows.Count > 0 ? dtyh.Rows[0]["S_YONGHUH"].ToString() : "";
            txtYONGHUM.Text = dtyh.Rows.Count > 0 ? dtyh.Rows[0]["S_YONGHUM"].ToString() : "";
            txtYuanShenJBH.Text = _SJXX.S_LAOBIANH;
            txtQuShuiDD.Text = _SJXX.S_JINGDIZ;
            cmbXingZhengQ.Value = _SJXX.S_XINGZHENGQBH;
            cmbLiuYu.Value = _SJXX.S_LIUYUBH;
            cmbShuiZiYFQ.Value = _SJXX.S_SHUIZIYBH;
            cmbShuiLiFP.Value = _SJXX.S_FENGPIANBH;
            cmbHuanXianBJ.Value = _SJXX.S_HUANGXIANBH;
            txtCengCi.Text = _SJXX.I_CENG.ToString();
            txtShenDu.Text = _SJXX.S_SHENGDU;
            txtKouJing.Text = _SJXX.I_KONGJING.ToString();
            txtXuKeZ.Text = _SJXX.S_XUKEZHENGBH;
            string zb = _SJXX.S_ZHUOBIAO.ToString();
            string[] arr = zb.Split('/');
            if (arr.Length > 0)
            {
                txtDu.Text = arr[0];
                txtFen.Text = arr[1];
                txtMiao.Text = arr[2];
                txtDu2.Text = arr[3];
                txtFen2.Text = arr[4];
                txtMiao2.Text = arr[5];
            }
            cmbJingLei.Value = _SJXX.S_JINGLEIXBH;
            cmbJingBJ.Value = _SJXX.S_JINGBIAOSBH;
            cmbHuanXianBJ.Value = _SJXX.S_HUANGXIANBH;
           // ckbJingXZ.Value = _SJXX.S_JINGZHUANGTBH;
            txtBeiZhu.Text = _SJXX.S_BEIZHU;
        }

        /// <summary>
        /// 保存验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            if (this.txtShenJingBH.Text == "")
            {
                PageTools.AlertMsg(this, "井编号不能为空！");
                this.txtShenJingBH.Focus();
                return false;
            }
            if (this.txtShenJingBH.Text.Length != 7 || !PageTools.ValidateString(this.txtShenJingBH.Text, PageTools.eValidateType.数字))
            {
                PageTools.AlertMsg(this, "井编号应为7位数字！");
                this.txtShenJingBH.Focus();
                return false;
            }
            if (this.txtQuShuiDD.Text == "")
            {
                PageTools.AlertMsg(this, "地址不能为空！");
                this.txtQuShuiDD.Focus();
                return false;
            }
            if (this.cmbXingZhengQ.Text == "")
            {
                PageTools.AlertMsg(this, "行政区不能为空！");
                this.cmbXingZhengQ.Focus();
                return false;
            }
            if (this.cmbLiuYu.Text == "")
            {
                PageTools.AlertMsg(this, "流域不能为空！");
                this.cmbLiuYu.Focus();
                return false;
            }
            if (this.cmbShuiZiYFQ.Text == "")
            {
                PageTools.AlertMsg(this, "水资源分区不能为空！");
                this.cmbShuiZiYFQ.Focus();
                return false;
            }
            if (this.cmbShuiLiFP.Text == "")
            {
                PageTools.AlertMsg(this, "水利分片不能为空！");
                this.cmbShuiLiFP.Focus();
                return false;
            }
            if (this.txtCengCi.Text == "")
            {
                PageTools.AlertMsg(this, "层不能为空！");
                this.txtCengCi.Focus();
                return false;
            }
            if (this.txtShenDu.Text == "")
            {
                PageTools.AlertMsg(this, "深度不能为空！");
                this.txtShenDu.Focus();
                return false;
            }
            if (this.txtKouJing.Text == "")
            {
                PageTools.AlertMsg(this, "口径不能为空！");
                this.txtKouJing.Focus();
                return false;
            }
            if (this.cmbJingBJ.Text == "")
            {
                PageTools.AlertMsg(this, "井标记不能为空！");
                this.cmbJingBJ.Focus();
                return false;
            }
            if (this.cmbHuanXianBJ.Text == "")
            {
                PageTools.AlertMsg(this, "请选择环线内外！");
                this.cmbHuanXianBJ.Focus();
                return false;
            }
            if (this.txtYuanShenJBH.Text == "")
            {
                PageTools.AlertMsg(this, "原井号不能为空！");
                this.txtYuanShenJBH.Focus();
                return false;
            }

            float x;
            if (!float.TryParse(txtBLL.Text, out x))
            {
                PageTools.AlertMsg(this, "泵流量必须填写数字！");
                this.txtBLL.Focus();
                return false;
            }
            return true;
        }

        #endregion
    }
}